﻿function loadScript() {
        var script = document.createElement('script');
        script.type = 'text/javascript';
        script.src = 'https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=true&key=AIzaSyALyu7_1ps4iLKxLOLhWF2ndz_s7zJsBKw&' +
            'callback=showMap';
        document.body.appendChild(script);
}

window.onload = loadScript;

function showMap() 
{
    //var myLatlng = new google.maps.LatLng(49.983540, 36.239416);

    var myLatlng2 = new google.maps.LatLng(50.021832, 36.224982);

    //var middle = new google.maps.LatLng((49.983540 + 50.022747) / 2.0, (36.239416 + 36.224797) / 2.0);

    var myOptions = {
        zoom: 18,
        center: myLatlng2,
        mapTypeControl: false,
        streetViewControl: false,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    var styles = [
    {
        "stylers": [
        { "saturation": -100 }
        ]
    }
    ];
    var styledMap = new google.maps.StyledMapType(styles, { name: "Styled Map" });
    var map = new google.maps.Map(document.getElementById("map"), myOptions);
    map.mapTypes.set('map_style', styledMap);
    map.setMapTypeId('map_style');

    //var marker = new google.maps.Marker({
    //    position: myLatlng,
    //    map: map
    //});

    var marker2 = new google.maps.Marker({
        position: myLatlng2,
        map: map
    });
}

function focusTA(id, value) {
    if (document.getElementById(id).value == value) {
        document.getElementById(id).value = "";
        document.getElementById(id).style.fontSize = "11px";
    }
}
function blurTA(id, value) {
    if (document.getElementById(id).value == "") {
        document.getElementById(id).value = value;
        document.getElementById(id).style.fontSize = "11px";
    }
}


$(document).ready(function () {
    $('.input input').focus(function () {
        $(this).parent().children("span").animate({ opacity: 0.7, left: -($(this).parent().children("span").width()) - 10 + "px" }, { duration: 0 });
    });
    $('.input span').click(function () {
        $(this).animate({ opacity: 0.3, left: -($(this).parent().children("span").width()) - 10 + "px" }, { duration: 0 });
        $(this).parent().children("input").focus();
    });
    $('.input input').blur(function () {
        if ($(this).val() == "") {
            $(this).parent().children("span").animate({ opacity: 1, left: "15px" }, { duration: 0 });
        } else {
            $(this).parent().children("span").animate({ opacity: 0 }, { duration: 0 });
        }
    });

    $('a.msgSender').click(function () {
        submitForm();
        return false;
    });

    $('textarea#messageTxt').blur(function(){
        blurTA('messageTxt', 'Добрый день, меня интересует...');
    });

    $('textarea#messageTxt').focus(function(){
        focusTA('messageTxt', 'Добрый день, меня интересует...');
    });

});

    function checkEmail(email) {
        var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
        return filter.test(email);
    }


    function submitForm() {

        clearError();
      
        var name       = document.getElementById("name").value;
        var email      = document.getElementById("e-mail").value;
        var phone      = document.getElementById("phone").value;
        var topic      = document.getElementById("topic").value;
        var messageTxt = document.getElementById("messageTxt").value;
        var challengeField = $('input#recaptcha_challenge_field').val();
        var responseField = $('input#recaptcha_response_field').val();


        if (name == "") {
            showError("name", "Ошибка: Введите своё имя");
        }
        else if (responseField == ""){
            showError("", "Ошибка: Введите контрольные слова");
        }
        else if (email == "" || !checkEmail(email)) {
            showError("e-mail", "Ошибка: Введите ваш e-mail");
        }
        else if (messageTxt == "" || messageTxt == "Добрый день, меня интересует...") {
            showError("messageTxt", "Ошибка:  Введите текст сообщения");
        }
        else {
            if (phone == "") { phone = "/"; }
            if (topic == "") { topic = "/"; }
            var obj = {
                Name: name,
                Email: email,
                Phone: phone,
                Topic: topic,
                Message: messageTxt,
                recaptcha_challenge_field: challengeField,
                recaptcha_response_field: responseField
            };

            $.ajax(
                {
                    url: location.origin + location.pathname + '/LeaveMessage',
                    type: 'POST',
                    contentType: 'application/json',
                    data: JSON.stringify({ message: obj }),
                    success: function (result) {
                        if (result && result.Passed) {
                            $('#cSuc').css('visibility', 'visible').hide().fadeIn(450);
                            $('#contact').animate({ paddingTop: "60px" }, 250);
                            document.getElementById("name").value = "";
                            $("#name").parent().children("span").animate({ opacity: 1, left: "15px" }, { duration: 0 });
                            document.getElementById("e-mail").value = "";
                            $("#e-mail").parent().children("span").animate({ opacity: 1, left: "15px" }, { duration: 0 });
                            document.getElementById("phone").value = "";
                            $("#phone").parent().children("span").animate({ opacity: 1, left: "15px" }, { duration: 0 });
                            document.getElementById("messageTxt").value = "";
                            $("#messageTxt").parent().children("span").animate({ opacity: 1, left: "15px" }, { duration: 0 });
                            document.getElementById("topic").value = "";
                            $("#topic").parent().children("span").animate({ opacity: 1, left: "15px" }, { duration: 0 });
                            document.getElementById("messageTxt").value = "Добрый день, меня интересует...";
                            $('input#recaptcha_response_field').val('');
                            Recaptcha.reload();
                        }
                        else {
                            showError("#name", "Ошибка сервера. " + result.ErrorMessage);
                        }
                    },
                    error: function (error, data, e) {
                        if (data == 'error') {
                            showError("#name", "Сервер недоступен. Попробуйте ещё раз");
                        }
                    }
                });
        }
    }


    function clearError() {
        $('.error').removeClass('error');
        $("#cError").text('');
        $('#cSuc').hide();
        $('#cError').hide();
    }

    function showError(id, errorMessage) {
        if (document.getElementById("cError").innerHTML == "") {
            document.getElementById("cError").innerHTML = errorMessage;
            $('#cError').css('visibility', 'visible').hide().fadeIn(650);
            $('#contact').animate({ paddingTop: "60px" }, 250);
        }
        else {
            //document.getElementById("cError").innerHTML = document.getElementById("cError").innerHTML + errorMessage
        }
        
        //if (document.getElementById(id).value == "" && id == "bericht") {
        //    document.getElementById(id).value = "UW BERICHT, VRAAG OF OPMERKING...";
        //}
        if (document.getElementById(id)) {
            document.getElementById(id).className = "error";
        }
    }
